package com.zm.spring.dependency.injection.setter.demo;

import com.zm.spring.dependency.injection.entity.UserHolder;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ApiDependencySetterInjectionDemo {


    public static void main(String[] args) {

        AnnotationConfigApplicationContext annotationConfigApplicationContext
                = new AnnotationConfigApplicationContext();
        BeanDefinition beanDefinition = createUserHolderBeanDefinition();
        annotationConfigApplicationContext.registerBeanDefinition("userHolder", beanDefinition);
        BeanDefinitionReader reader = new XmlBeanDefinitionReader(annotationConfigApplicationContext);
        reader.loadBeanDefinitions("classpath:/META-INF/spring-ioc.xml");
        annotationConfigApplicationContext.refresh();
        UserHolder userHolder = annotationConfigApplicationContext.getBean(UserHolder.class);
        System.out.println(userHolder);


        annotationConfigApplicationContext.close();

    }

    private static BeanDefinition createUserHolderBeanDefinition() {
        BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(UserHolder.class);
        definitionBuilder.addPropertyReference("userPO", "userPO");
        return definitionBuilder.getBeanDefinition();
    }

}
